
-- This file contains all the functions relating to controlling the player as they move between areas defined by the func_spacerace entities

function showMessage(ply, message)
    ply:PrintMessage( HUD_PRINTCENTER, message)
end

function spawn_team_admin( ply )	
    ply:SetTeam( TEAM_ADMIN )
    ply:Spawn()
    local message = "SpaceRace welcomes our Admin Overlord " .. ply:Nick()
    showMessage(ply, message )
end
 
function spawn_team_spectator( ply )
 
    join_team_spectator( ply )
    ply:Spawn()
end

function spawn_team_builder( ply )

    ply:SetTeam( TEAM_BUILDER )
    ply:Spawn()
end

function SetPlayerLoadout( ply )

    ply:StripWeapons()

    if ply:Team() == TEAM_JOINING then
       --has nothing
    
    elseif ply:Team() == TEAM_SPECTATOR then
        --ply:Give( "gmod_camera" )
        
    elseif ply:Team() == TEAM_BUILDER then
        ply:Give( "weapon_physcannon" )
        ply:Give( "weapon_physgun" )
        ply:Give( "gmod_tool" )
    
    elseif ply:Team() == TEAM_ENTRANT then
        ply:Give( "weapon_physcannon" )
        ply:Give( "weapon_physgun" )
        
    elseif ply:Team() == TEAM_RACER then
        --has nothing
    
    elseif ply:Team() == TEAM_ADMIN then
        ply:Give( "weapon_physcannon" )
        ply:Give( "weapon_physgun" )
        ply:Give( "weapon_ar2" )
        ply:Give( "weapon_crossbow" )
        ply:Give( "weapon_357" );
        ply:Give( "gmod_tool" )
    end
end

function join_team_spectator( ply )
 
    ply:SetTeam( TEAM_SPECTATOR )
    SetPlayerLoadout( ply )

end

function entered_build_area( ply )
    
    ply:SetTeam( TEAM_BUILDER )
    
    if !ply:HasWeapon("weapon_physgun") then
        SetPlayerLoadout( ply )
    elseif !ply:HasWeapon("gmod_tool") then
        ply:Give("gmod_tool")
    end
end

function left_build_area( ply )

    if ply:Team() == TEAM_BUILDER then
        join_team_spectator( ply )
    end
end

function entered_grid( ply )

    ply:SetTeam( TEAM_ENTRANT )
    if !ply:HasWeapon("weapon_physgun") then
        SetPlayerLoadout( ply )
    elseif ply:HasWeapon("gmod_tool") then
        ply:StripWeapon("gmod_tool")
    end
end

function left_grid( ply )

    if ply:Team() == TEAM_ENTRANT then
        join_team_spectator( ply )
    end
end

function mapZoneEnter(ply , zone , checkpoint_num)
        
        if GetConVarNumber("sr_debug") == 1 then print("SR MZE checkpoint_num: "..tostring(checkpoint_num)) end
        
        local ZoneNumber = tonumber(zone)
        
        if ply:IsPlayer() then     
            if ZoneNumber == SR_BUILD_AREA then
                showMessage(ply,"Build Area")
                entered_build_area(ply)
            elseif ZoneNumber == SR_START_GRID then
                if ply:Team() == TEAM_BUILDER then showMessage(ply,"Starting Grid") end
                entered_grid(ply)
            elseif ZoneNumber == SR_RACE_START then
                --showMessage(ply,"Race Started")
                started_race(ply)
            elseif ZoneNumber == SR_RACE_FINISH then
                --showMessage(ply,"Race Finished")
                finished_race(ply)
            elseif ZoneNumber == SR_CHECKPOINT then
                checkpoints = tonumber(GetGlobalInt( "checkpoints" ))
                checkpoint(ply , checkpoint_num)
            else
                print("SR MapZoneEnter: Unknown zone: "..tostring(ZoneNumber))
            end
        else
            print("SR MapZoneEnter: Invalid player")
        end
end

function mapZoneLeave(ply , zone)
            
        local ZoneNumber = tonumber(zone)
        if ply:IsPlayer() then
            if ZoneNumber == SR_BUILD_AREA then
                left_build_area(ply)
            elseif ZoneNumber == SR_START_GRID then
                left_grid(ply)
            elseif ZoneNumber == SR_RACE_START then
                --do nothing
            elseif ZoneNumber == SR_RACE_FINISH then
                --do nothing
            elseif ZoneNumber == SR_CHECKPOINT then
                --do nothing
            else
                print("SR MapZoneLeave: Unknown zone: "..tostring(ZoneNumber))
            end
        else
            print("SR MapZoneLeave: Invalid player")
        end
end

function register_concommands()
        
    concommand.Add( "sr_clear_best", clear_best_time )
    
    concommand.Add( "sr_best", show_best_time )
    
    concommand.Add( "sr_times", send_times_table )
    
    print("=====SpaceRace ConCommands Registered=======")
end

function GM:ShowHelp( ply ) -- This hook is called everytime F1 is pressed.
    ply:ConCommand( "sr_menu" )
end --Ends function

function GM:ShowTeam( ply ) -- This hook is called everytime F2 is pressed.
    ply:ConCommand( "sr_best" )
end --Ends function

function GM:ShowSpare1( ply ) -- This hook is called everytime F3 is pressed.
    ply:ConCommand( "sr_clear_best_time" )
end --Ends function

function GM:ShowSpare2( ply ) -- This hook is called everytime F4 is pressed.
    ply:ConCommand( "sr_times" )
end --Ends function
